/*
 * Enemy.cpp
 *
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses 
 */

#include "Enemy.h"
#include <cstdlib>
#include <ctime>
#include "ModelConstants.h"

Enemy::Enemy(int x, int y)
    : MovingObject(x, y) {
    init();
}

Enemy::Enemy(int id, int x, int y)
    : MovingObject(id, x, y) {
    init();
}

void Enemy::init() {
    spawn_x = x;
    spawn_y = y;
    range = 0;
    alignment = AL_ENEMY;
}

int Enemy::getRange() {
    return range;
}

int Enemy::scoutForBomberman(Map& map) {
    int bomberman_direction = NO_DIRECTION;
    if (scoutDirection(map, UP)) {
        bomberman_direction = UP;
    } else if (scoutDirection(map, DOWN)) {
        bomberman_direction = DOWN;
    } else if (scoutDirection(map, RIGHT)) {
        bomberman_direction = RIGHT;
    } else if (scoutDirection(map, LEFT)) {
        bomberman_direction = LEFT;
    }
    return bomberman_direction;
}

bool Enemy::collideWith(MapObject* object) {
    if (object->getAlignment() == AL_PLAYER) {
        object->destroy();
    }
    return object->getCollidedBy(this);
}

bool Enemy::getCollidedBy(MapObject* object) {
    if (object->getAlignment() == AL_PLAYER) {
        object->destroy();
    }
    return isDestroyed();
}

int Enemy::horizontalDistanceToSpawn() {
    return spawn_x - x;
}

int Enemy::verticalDistanceToSpawn() {
    return spawn_y - y;
}

int Enemy::getRandomDirection() {
    srand(time(NULL));
    int new_direction = direction;
    int loop_counter = 100;  // Tries 100 times, and returns the current direction
    while ((new_direction == direction) && (loop_counter > 0)) {
        int n = rand()%4;
        if (n == 1) {
            new_direction = UP;
        } else if (n == 2) {
            new_direction = RIGHT;
        } else if (n == 3) {
            new_direction = DOWN;
        } else {
            new_direction = LEFT;
        }
        loop_counter--;
    }
    return new_direction;
}

Enemy* Enemy::clone() {
    return new Enemy(*this);
}

bool Enemy::scoutDirection(Map& map, int direction) {
    int x = this->x;
    int y = this->y;
    for (int c = range; c != 0; c--) {
        if (direction == RIGHT) {
            x++;
        } else if (direction == LEFT) {
            x--;
        } else if (direction == UP) {
            y--;
        } else {
            y++;
        }
        // Tests if the edge of the map has been reached
        if (! map.isValidPosition(x, y)) {
            return false;
        }
        MapObject* object = map.getObject(x, y);
        // If it found an object, checks if it's a player object
        if (object != 0) {
            if (object->getAlignment() == AL_PLAYER){
                return true;
            } else {
                return false;
            }
        }
    }
    return false;
}

Enemy::~Enemy() {

}
